package com.blyat.xsoft.kernel.component.mybatis.dialect;

import java.util.Set;

/**
 * @author syh
 * @date 2019/8/26
 **/
public class OracleDialect extends AbstractDialect {
    public OracleDialect() {
    }

    public boolean supportsLimit() {
        return true;
    }

    public String getLimitString(String sql, int offset, int limit) {
        return this.getLimitString(sql, offset, Integer.toString(offset), Integer.toString(limit));
    }

    public String getLimitString(String sql, int offset, String offsetPlaceholder, String limitPlaceholder) {
        sql = sql.trim();
        boolean isForUpdate = false;
        if (sql.toLowerCase().endsWith(" for update")) {
            sql = sql.substring(0, sql.length() - 11);
            isForUpdate = true;
        }

        StringBuilder pagingSelect = new StringBuilder(sql.length() + 100);
        if (offset > 0) {
            pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
        } else {
            pagingSelect.append("select * from ( ");
        }

        pagingSelect.append(sql);
        if (offset > 0) {
            String endString = offsetPlaceholder + "+" + limitPlaceholder;
            pagingSelect.append(" ) row_ where rownum <= " + endString + ") where rownum_ > ").append(offsetPlaceholder);
        } else {
            pagingSelect.append(" ) where rownum <= " + limitPlaceholder);
        }

        if (isForUpdate) {
            pagingSelect.append(" for update");
        }

        return pagingSelect.toString();
    }

    @Override
    protected String getSqlPattern() {
        return "\\blike\\b\\s*(['\"]%['\"]\\s*\\|{2})?\\s*(#\\{\\s*\\w+\\s*\\})\\s*(\\|{2}['\"]%['\"])?";
    }

    @Override
    protected String modifySql(String sql, Set<String> replaceEscape) {
        return null;
    }
}
